package com.fie.graphing;

import java.awt.*;
import java.net.*;

public class DrawStaticGraph extends Canvas {
       Image image;
       int width, height;
       int counter = 0;

       public DrawStaticGraph(String urlname) {
               //
               // Canvas is a subclass of Component, so you can
               // use the getToolkit() method in class Component to
               // get a Toolkit object.  You then use the Toolkit to
               // create an Image from the URL name
               //
               Toolkit tk = getToolkit();
               try {
                       //
                       // If the urlname is a valid URL, call the
                       // URL version of getImage()
                       //
                       URL url = new URL(urlname);
                       image = tk.getImage(url);
                       
               } catch (MalformedURLException e) {
                       //
                       // Otherwise use the filename version of getImage()
                       //
                       image = tk.getImage(urlname);
               }

               //
               // Force the image to be loaded, don't proceed until
               // the picture is all loaded and the size is known
               // prepareImage() returns true when the image has been
               // loaded.
               //
               while (!tk.prepareImage(image, -1, -1, this)) {
                       try {
                               Thread.sleep(500);
                               System.out.println("Loading...");
                               counter++;
                               if(counter > 7)
                                   break;
                       } catch (Exception e) {}
               }
               width = image.getWidth(this);
               height = image.getHeight(this);
       }

       public void paint(Graphics g) {
               g.drawImage(image, 0, 0, this);
       }

       //
       // The following two methods are needed so Java knows how
       // big the canvas ought to be.  Without them, the window
       // occupies the entire screen.
       //
       public Dimension minimumSize() {
               return new Dimension(width, height);
       }

       public Dimension preferredSize() {
               return minimumSize();
       }

}